/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pokemon.services;

import com.pokemon.dao.PokemonDAO;
import com.pokemon.dto.DTOConvertor;
import com.pokemon.dto.PokemonDTO;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Ladislav
 */
@Service("pokemonManager")
@Transactional
public class PokemonServiceImpl implements PokemonService{

    @Autowired
    private PokemonDAO pokemonDAO;
   
    @Required
    @Autowired
    public void setPokemonDAO(PokemonDAO pokemonDAO)
    {
        this.pokemonDAO = pokemonDAO;
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @Transactional
    public PokemonDTO addPokemon(PokemonDTO pokemon) {
        pokemonDAO.addPokemon(DTOConvertor.dtoToEntity(pokemon));
        return pokemon;
    }

    @Transactional
    public PokemonDTO getPokemon(Long id) {
        return DTOConvertor.entityToDto(pokemonDAO.getPokemon(id));
    }
   
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @Transactional
    public PokemonDTO updatePokemon(PokemonDTO pokemon) {
        pokemonDAO.updatePokemon(DTOConvertor.dtoToEntity(pokemon));
        return pokemon;
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @Transactional
    public PokemonDTO removePokemon(PokemonDTO pokemon) {
        pokemonDAO.removePokemon(DTOConvertor.dtoToEntity(pokemon));
        return pokemon;
    }

    @Transactional
    public List<PokemonDTO> getAllPokemons() {
        return DTOConvertor.pokemonEntityListToDtoList(pokemonDAO.getAllPokemons());
    }
}

